(function() {

    var utilNs = window.namespace('shchekoldin.util');

    var HtmlTemplate = {

        BRANCH :
            '<div class="branchesListItem">' +
                '<div class="branchesListItemMarker ${data.markerClass}"></div>' +
                '<div>Branch Key: ${data.branchKey}</div>' +
                '<div>Branch Status: ${data.branchStatus}</div>' +
                '<div>Branch Last Revision: ${data.branchLastRevision}</div>' +
                '<div>Issue Assignee: ${data.issueAssignee}</div>' +
                '<div>Issue Reporter: ${data.issueReporter}</div>' +
                '<div>Issue Status: ${data.issueStatus}</div>' +
                '<div>Issue Summary: ${data.issueSummary}</div>' +
                '</div>'

    };

    function addMarkerClassAndSortMarker(branches) {
        var branch;

        for (var i = 0, len = branches.length; i < len; ++i) {
            branch = branches[i];
            branch.markerClass = 'branchesListItemMarkerIgnore';
            branch.sortMarker = 5;

            if (((branch.branchStatus == 'OPEN') || (branch.branchStatus == 'INACTIVE')) &&
                ((branch.issueStatus == 'RESOLVED') || (branch.issueStatus == 'CLOSED'))) {
                branch.markerClass = 'branchesListItemMarkerError';
                branch.sortMarker = 1;
            }

            if ((branch.branchStatus == 'CLOSED') &&
                ((branch.issueStatus == 'RESOLVED') || (branch.issueStatus == 'CLOSED'))) {
                branch.markerClass = 'branchesListItemMarkerOk';
                branch.sortMarker = 4;
            }

            if ((branch.branchStatus == 'CLOSED') &&
                (branch.issueStatus != 'RESOLVED') && (branch.issueStatus != 'CLOSED')) {
                branch.markerClass = 'branchesListItemMarkerWarning';
                branch.sortMarker = 2;
            }

            if (branch.issueStatus == null) {
                branch.markerClass = 'branchesListItemMarkerNullIssue';
                branch.sortMarker = 3;
            }
        }
    }

    function renderBranches(branches) {
        addMarkerClassAndSortMarker(branches);

        branches.sort(function(o1, o2) {
            if (o1.sortMarker < o2.sortMarker) {
                return -1;
            }

            if (o1.sortMarker > o2.sortMarker) {
                return 1;
            }

            return 0;
        });

        var result = '', branch;

        for (var i = 0, len = branches.length; i < len; ++i) {
            branch = branches[i];

            result += utilNs.string.format(HtmlTemplate.BRANCH, {
                data : branches[i]
            });
        }

        $('.branchesList').append('<h1>Top 100 branches</h1>');
        $('.branchesList').append(result);
    }

    $.ajax({
        url : '/rest/branch/list/0/100/',

        success : function(response) {
            $('.branchesList').removeClass('branchesListLoading');
            renderBranches(response);
        },

        error : function() {
            // Show some error message
        }
    });

})();
